export class StringUtils {
    private static baseClassPattern = /java\.(util|lang)\./;
    static SIMPLE_TYPE_SET = new Set<string>([
        'boolean',
        'Boolean',
        'int',
        'Integer',
        'long',
        'Long',
        'double',
        'Double',
        'char',
        'Character',
        'byte',
        'Byte',
        'String'
    ]);

    public static isPrimitiveName(entityName: string): boolean {
        return entityName.indexOf('java') === 0 || StringUtils.SIMPLE_TYPE_SET.has(entityName);
    }

    public static toSimpleTypeName(typeName: string): string {
        if (StringUtils.isNullOrEmpty(typeName)) {
            return '';
        }
        while (true) {
            const matcher = StringUtils.baseClassPattern.exec(typeName);
            if (matcher) {
                typeName = typeName.replace(StringUtils.baseClassPattern, '');
            } else {
                return typeName;
            }
        }
    }

    public static repeat(str: string, count: number) {
        return str.repeat(count);
    }

    public static isNullOrEmpty(str: string) {
        if (!str) {
            return false;
        }
        return !str && str.length === 0;
    }

    public static repeatT(count: number): string {
        return '    '.repeat(count);
    }

    public static getFirstLineStr(str: string): string {
        if (!str) {
            return '--';
        }
        let simpleLine = str;
        const turnLineIndex = simpleLine.indexOf('\n');
        if (turnLineIndex > -1) {
            simpleLine = simpleLine.substr(0, turnLineIndex);
        }
        return simpleLine;
    }

    static contains(str: string, containStr: string): boolean {
        return str && str.indexOf(containStr) > -1;
    }

    static startWith(content: string, str: string) {
        if (!content || !str) {
            return false;
        }
        return content.indexOf(str) === 0;
    }
}
